package DAO.Servlet;

import DAO.JavaBean.CustomerBean;
import DAO.JavaDAO.CustomerDAO;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet(value = "/insertCustomer")
public class InsertCustomerServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        CustomerBean customer = new CustomerBean(
                request.getParameter("custName"),request.getParameter("email"),request.getParameter("phone")
        );
        try {
            CustomerDAO customerDAO = new CustomerDAO();
            if(customerDAO.insertCustomer(customer)){
                response.sendRedirect("DAO/showCustomer.jsp");
                } else {
                    response.sendRedirect("jsp/errorPage.jsp");
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
